package com.romadan.gui.controller.impl;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import com.romadan.core.nonlinearanalys.fet.volterra.logic.NLParameters;
import com.romadan.general.settings.ProjectState;
import com.romadan.gui.controller.AbstractController;
import com.romadan.utils.math.Complex;

public class NonlinearAnalysisTabController extends AbstractController implements Initializable {
	private ProjectState pstate = new ProjectState();
	private int mOrder = 2;
	private boolean calcCross = false;

	public void initialize(URL url, ResourceBundle rb) {
		cbMOrder.getItems().clear();
		cbMOrder.getItems().add(2);
		cbMOrder.getItems().add(3);
		cbMOrder.getSelectionModel().select(0);
		
		lblCoefficients.setWrapText(true);
		
		//temp
		txtAmplitude1.setText("1");
		txtAmplitude2.setText("-1");
		txtFrequency1.setText("1000");
		txtFrequency2.setText("1500");
		txtVoltage12.setText("0.0565225825+j0.08764564");
		txtVoltage22.setText("-0.000753898+j0.000599333");
		txtVoltage122.setText("5.07173094378E-4+j3.61308375993E-4");
		txtVoltage13.setText("0.01394039849-j0.01520846614");
		txtVoltage23.setText("-1.10646501268E5-j1.42240794089E-5");
	}
	
	@FXML
	private void clickOnStart() {
		super.info("Nonlinear analysis for Volterra Model was started ...");
		Map<String, String> params = new HashMap<String, String>();
		params.put("txtAmplitude1", txtAmplitude1.getText());
		params.put("txtAmplitude2", txtAmplitude2.getText());
		params.put("txtFrequency1", txtFrequency1.getText());
		params.put("txtFrequency2", txtFrequency2.getText());
		params.put("txtVoltage12", txtVoltage12.getText());
		params.put("txtVoltage22", txtVoltage22.getText());
		params.put("txtVoltage13", txtVoltage13.getText());
		params.put("txtVoltage23", txtVoltage23.getText());
		params.put("txtVoltage122", txtVoltage122.getText());
		
		NLParameters nlp = actions.executeNonlinearAnalysisForVolterra(params);
		if (nlp != null) {
			Map<String, Complex> nlElements = nlp.getElements();
			String result = "";
			for (String name : nlElements.keySet()) {
				Complex value = nlElements.get(name);
				if (!value.equals(Complex.ZERO)) {
					result += name + " = " + value.toString() + "\n";
				}
			}
			
			lblCoefficients.setText(result);
			
			txtGm1.setText(nlp.getGm1().toString());
			txtGm2.setText(nlp.getGm2().toString());
			txtGm3.setText(nlp.getGm3().toString());
			txtGd1.setText(nlp.getGd1().toString());
			txtGd2.setText(nlp.getGd2().toString());
			txtGd3.setText(nlp.getGd3().toString());
			txtGmd.setText(nlp.getGmd().toString());
			txtCgs1.setText(nlp.getCgs1().toString());
			txtCgs2.setText(nlp.getCgs2().toString());
			txtCgs2.setText(nlp.getCgs2().toString());
			txtCgs3.setText(nlp.getCgs3().toString());
			txtCgd1.setText(nlp.getCgd1().toString());
		}
		
		super.info("Nonlinear analisys is finished. See results.");
	}
	
	@FXML
	private void changeMOrder() {
		mOrder = cbMOrder.getSelectionModel().getSelectedItem();
		if (mOrder == 2) {
			lblVoltage13.setVisible(false);
			txtVoltage13.setVisible(false);
			lblVoltage23.setVisible(false);
			txtVoltage23.setVisible(false);
		} else if (mOrder == 3) {
			lblVoltage13.setVisible(true);
			txtVoltage13.setVisible(true);
			lblVoltage23.setVisible(true);
			txtVoltage23.setVisible(true);
		}
		pstate.setmOrder(mOrder);
	}
	
	@FXML
	private void changeCheck() {
		calcCross = checkCross.isSelected();
		if (calcCross) {
			lblVoltage122.setVisible(true);
			txtVoltage122.setVisible(true);
		} else {
			lblVoltage122.setVisible(false);
			txtVoltage122.setVisible(false);
		}
		pstate.setCalcCrossCoefficients(calcCross);
	}
	
	@FXML
	private TextField txtAmplitude1;
	@FXML
	private TextField txtAmplitude2;
	@FXML
	private TextField txtFrequency1;
	@FXML
	private TextField txtFrequency2;
	@FXML
	private TextField txtVoltage12;
	@FXML
	private TextField txtVoltage22;
	@FXML
	private TextField txtVoltage122;
	@FXML
	private TextField txtVoltage13;
	@FXML
	private TextField txtVoltage23;
	@FXML
	private TextField txtGm1;
	@FXML
	private TextField txtGm2;
	@FXML
	private TextField txtGm3;
	@FXML
	private TextField txtGd1;
	@FXML
	private TextField txtGd2;
	@FXML
	private TextField txtGd3;
	@FXML
	private TextField txtGmd;
	@FXML
	private TextField txtCgs1;
	@FXML
	private TextField txtCgs2;
	@FXML
	private TextField txtCgs3;
	@FXML
	private TextField txtCgd1;
	
	@FXML
	private Label lblVoltage12;
	@FXML
	private Label lblVoltage22;
	@FXML
	private Label lblVoltage122;
	@FXML
	private Label lblVoltage13;
	@FXML
	private Label lblVoltage23;
	@FXML
	private Label lblCoefficients;
	
	@FXML
	private Button btnStart;
	
	@FXML
	private ComboBox<Integer> cbMOrder;
	
	@FXML
	private CheckBox checkCross;
}
